<?php

namespace App\Http\Controllers;

use App\Models\GrReply;
use App\Models\GrUserComment;
use App\service\ReviewImages;
use Dcat\Admin\Contracts\LazyRenderable;
use Dcat\Admin\Traits\LazyWidget;
use Dcat\Admin\Widgets\Form;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Cache;
use Illuminate\Support\Facades\DB;

class Reply extends Form implements LazyRenderable
{
    use LazyWidget;

    public function handle(array $input)
    {
        DB::beginTransaction();
        try {
            $replay_content=$input['replay_content'];
            $reviewImage = new ReviewImages();
            $res = $reviewImage->text($replay_content);
            if ($res != "合规") {
                return $this->response()->error('文本不合法');
            }
            $comment_id=Cache::get('comment_id');
            $user_id=Cache::get('user_id');
            $goods_id=Cache::get('goods_id');
            $data=['goods_id'=>$goods_id,'reply_content'=>$replay_content,'pid'=>0,
                'comment_id'=>$comment_id,'by_reply_user_id'=>$user_id,'reply_user_id'=>2];
            $res=GrReply::insert($data);
            if ($res){
               $res= GrUserComment::where('id',$comment_id)->update(['replay_status'=>1]);
            }
            DB::commit();
            if($res){
                return $this->response()->success('评论成功');
            }
        }catch (\Exception $e){
            DB::rollBack();
        }


    }
    public function default()
    {
        // 展示上个页面传递过来的值
        $comment_id=$this->payload['key'];
        $user_id=$this->payload['user_id'];
        $goods_id=$this->payload['goods_id'];
        Cache::put('comment_id',$comment_id);
        Cache::put('user_id',$user_id);
        Cache::put('goods_id',$goods_id);
        return [
            'id' => $this->payload['id'] ?? '',
            'user_id' => $this->payload['user_id'] ?? '',
            'goods_id' => $this->payload['goods_id'] ?? '',
        ];
    }
    public function form()
    {
        $this->text('replay_content')->required()->help('评论内容');

    }


}
